
$(function () {
    nav_current();
    download_nav();
   	dialog();
    
});

function nav_current() {
    var header_bottom_content = $('.header_bottom_content');
    header_bottom_content.find('a').each(function () {
        var $nav_href = $(this).attr('href');
        var local_attr = $nav_href.substr($nav_href.lastIndexOf('/') + 1);
        
        var href_attr = window.location.href.substr(window.location.href.lastIndexOf('/') + 1).replace(/\?.+/, "");
        if (local_attr.toLocaleLowerCase() == href_attr.toLocaleLowerCase()) {
            $(this).attr('class', 'current');
        }
    });
}

function download_nav() {
    var i=$('ul.tabs_ul li').size();
    console.log(i);

    var tabid=GetQueryString("id");
    
    var tab=window.location.hash;
    
    // if(tab=="#tabs2"||tabid=='1'){
    //     var _showTab = 1;
    // }else if(tab=="#tabs3"||tabid=='2'){
    //     var _showTab = 2;
    // }else if(tab=="#tabs4"||tabid=='3'){
    //     var _showTab = 3;
    // }else if(tab=="#tabs5"||tabid=='4'){
    //     var _showTab = 4;
    // }else{
    //     var _showTab = 0;
    // }
    if(i==1){
        var _showTab = 0;

    }else if(i==5){
        if(tab){
        switch (tab)
        {
        case "#tabs2":
          var _showTab = 1;
          break;
        case "#tabs3":
          var _showTab = 2;
          break;
        case "#tabs4":
          var _showTab = 3;
          break;
        case "#tabs5":
          var _showTab = 4;
          break;
        default:
          var _showTab = 0;
        }
    }else{
        switch (tabid)
        {
        case "1":
          var _showTab = 1;
          break;
        case "2":
          var _showTab = 2;
          break;
        case "3":
          var _showTab = 3;
          break;
        case "4":
          var _showTab = 4;
          break;
        default:
          var _showTab = 0;
        }
    }
    }
    
    
     
    console.log(_showTab);
    $('.tabs_box').each(function () {
        var $tab = $(this);
        

        var $defaultLi = $('ul.tabs_ul li', $tab).eq(_showTab).addClass('active');
        
        $($defaultLi.find('a').attr('href')).siblings().hide();
        $('ul.tabs_ul li', $tab).click(function () {
            var $this = $(this),
                _clickTab = $this.find('a').attr('href');
                
                /*if(window.location.href){
                window.location.href=_clickTab;
                }else{
                    window.location.href=window.location.href+_clickTab;
                }*/
                
            $this.addClass('active').siblings('.active').removeClass('active');
            $(_clickTab).stop(false, true).fadeIn().siblings().hide();
            return false;
        }).find('a').focus(function () {
            this.blur();
        });
    });
}
// dialog

$('.dialog_close').on('click',function(){
    var dialog_bg = $('.dialog_bg');
    var dialog = $('.dialog');
    dialog_bg.hide();
    dialog.hide();
    location.reload();
});

//编辑sdk
    $('.bj').on('click', function () {
        
        
        var info = $(this).data('info');
        var url="/Index/edit" ;
        $('.sp_name').html('编辑');
        console.log(url);
        $('#bjqdxx_qdm').val(info.name);
        $('#bjqdxx_cjsj').val(info.introduction);
        $('#bjqdxx_qdbm').val(info.state);  
        $('.aa').show(); 
        $('.dialog_bg').show(); 
        $('#bjqdxx_btn').on('click', function () {
        
        var name =$('#bjqdxx_qdm').val();
        var intro = $('#bjqdxx_cjsj').val();
        var state = $('#bjqdxx_qdbm').val();
         
        $.ajax({
            type: 'post',
            data: {
                id:info.id,
                name:name,
                intro:intro,
                state:state

            },
            url: url,
            dataType: 'text',
            success: function (response) {
                alert(response);
                //window.location.href="/YMN.php/Admin/Index/index.php#tabs1";
                location.reload(true);
            }
        });
    });
    });
//新增sdk
$('.download_url').on('click', function () {
        var url="/Index/add_sdk" ;
        $('.sp_name').html('新增下载内容');
        //console.log(info);
        $('#bjqdxx_qdm').val('');
        $('#bjqdxx_cjsj').val('');
        $('#bjqdxx_qdbm').val('0');  
        $('.aa').show(); 
        $('.dialog_bg').show(); 
        

        $('#bjqdxx_btn').on('click', function () {

        var name =$('#bjqdxx_qdm').val();
        var intro = $('#bjqdxx_cjsj').val();
        var state = $('#bjqdxx_qdbm').val();

        $.ajax({
            type: 'post',
            data: {
                //id:info.id,
                name:name,
                intro:intro,
                state:state

            },
            url: url,
            dataType: 'text',
            success: function (response) {
                alert(response);
                //location.reload(true);
                //window.location.href="/YMN.php/Admin/Index/index.php#tabs1";
                location.reload(true);
            }
        });
    });
    });
//版本编辑

$('.bbbj').on('click', function () {
        $('#form').attr('action','/Add/edit_version');
        var arr = $(this).data('arr');
        var url="/Add/edit_version" ;
        
        $('.sp_name').html('新增版本');
        
        // $('#uplaod_sdk').val(info.name);
        $('#bjqdxx_qdm').val(arr.name);
        $('#bjxx_bbh').val(arr.version);    
        $('#bjqdxx_cjsj').val(arr.introduction);
        id=arr.id;
        $('#id').val(id);
        $('.aa').show(); 
        $('.dialog_bg').show(); 

    });
//添加新版本
/*$('.tjxbb').on('click', function () {
        $('#form').attr('action','/YMN.php/Admin/Index/add_newversion');
        $('.sp_name').html('新增版本');
        
        
        $('#bjqdxx_qdm').val('');
        $('#bjxx_bbh').val('');
        $('#bjqdxx_cjsj').val('');
        $('.aa').show();
        
        
        // $('#uplaod_sdk').val(info.name);
        id=arr.id;
        $('#id').val(id);
 
    });*/
//新增版本
$('.xzbb').on('click', function () {
    
        $('#form').attr('action','/Add/add_newversion');
        $('.sp_name').html('新增版本');
        var id = $(this).data('s');
        var url="/Add/add_newversion" ;
        $('#bjqdxx_qdm').val('');
        $('#bjxx_bbh').val('');
        $('#bjqdxx_cjsj').val('');
        $('.aa').show();
        $('.dialog_bg').show();
        
        
        // $('#uplaod_sdk').val(info.name);

        
        $('#id').val(id);
        $('form').submit(function(){
 
            if(($('#bjqdxx_qdm').val())==0){
                alert('名称不能为空');
                return false; 
            }else if($('#bjxx_bbh').val()==0){
                alert('版本号不能为空');
                return false; 
            }else if($('#bjqdxx_cjsj').val()==0){
                alert('更新说明不能为空');
                return false;
            }
                 
        });
 
    });
//上线下线
 $('.xzbb_sx').on('click', function () {
        
        
        var id = $(this).data('ssid');
        var url="/Add/sdk_state" ;
        $.ajax({
            type: 'post',
            data: {
                id:id,

            },
            url: url,
            dataType: 'text',
            success: function (response) {
                alert(response);
                location.reload(true);
            }
        });
    
    });

 //发布
 $('.fb').on('click', function () {
        var fb = $(this).data('fb');
        var id = fb.id;
        var name = fb.name;
        var version = fb.version;
        var state = fb.state
        var url="/Index/release" ;
        
       
        if(!version){
            alert(name+"没有版本可发布，请添加版本。");
        }else{
            if(state==0){
                if(confirm("是否发布"+name+version+"v1.033？")){
                    $.ajax({
                        type: 'post',
                        data: {
                            id:id

                        },
                        url: url,
                        dataType: 'text',
                        success: function (data) {
                            alert(data);
                            //location.reload();
                            window.location.href="/Index/index";
                            //location.reload(true);
                        }
                    });
                }else{
                    return false;
                }

            }else{
                if(confirm(name+version+"目前为不开放下载状态，是否继续发布？")){
                    $.ajax({
                        type: 'post',
                        data: {
                            id:id

                        },
                        url: url,
                        dataType: 'text',
                        success: function (data) {
                            alert(data);
                            //location.reload();
                            //window.location.href="/YMN.php/Admin/Index/index.php#tabs1";
                            location.reload(true);
                        }
                    });
                }else{
                    return false;
                }
            }
        } 
            
        
        
        
    
    });
 //编辑渠道列表

    $('.bjlist').on('click', function () {
        
        
        var info = $(this).data('info');
        var url="/Support/edit_list" ;
        $('.sp_name').html('编辑');
        //console.log(info);
        $('#bjqdbb_qdm').val(info.name);
        $('#bjqdbb_lx').val(info.type);
        $('#bjqdbb_bbh').val(info.version);
        $('#bjqdbb_qdwz').val(info.channel_url);
        $('#bjqdbb_cslj').val(info.link_url);
        $('#bjqdbb_jj').val(info.intro);
        $('#bjqdbb_qdbm').val(info.state);
        // $('#bjqdxx_cjsj').val(info.introduction);
        // $('#bjqdxx_qdbm').val(info.state);  
        $('.bb').show(); 
        $('.dialog_bg').show(); 
        $('#bjqdbb_btn').on('click', function () {

        var name =$('#bjqdbb_qdm').val();
        var type =$('#bjqdbb_lx').val();
        var version =$('#bjqdbb_bbh').val();
        var channel_url =$('#bjqdbb_qdwz').val();
        var link_url =$('#bjqdbb_cslj').val();
        var intro = $('#bjqdbb_jj').val();
        var state = $('#bjqdbb_qdbm').val();
        if(($('#bjqdbb_qdm').val())==0){
                alert('名称不能为空');
                return false; 
            }else if($('#bjqdbb_bbh').val()==0){
                alert('版本号不能为空');
                return false;
            }
         
        $.ajax({
            type: 'post',
            data: {
                id:info.id,
                name:name,
                type:type,
                version:version,
                channel_url:channel_url,
                link_url:link_url,
                intro:intro,
                state:state

            },
            url: url,
            dataType: 'text',
            success: function (response) {
                alert(response);
                //if(GetQueryString("id")=='1'){
                    //alert(GetQueryString("id")!=='1');
                    //window.location.href="/YMN.php/Admin/Index/index.php#tabs2";
                //}else{
                    //location.reload();
                    //window.location.href = window.location.href.replace('/Index/index#tabs2');
                    //window.location.href="/Index/index";
                    location.reload(true);
                //}
                  
            }
        });
    });
    });
//新增渠道
    $('.xzqdlb').on('click', function () {
        var name =$('#bjqdbb_qdm').val('');
        var type =$('#bjqdbb_lx').val(0);
        var version =$('#bjqdbb_bbh').val('');
        var channel_url =$('#bjqdbb_qdwz').val('');
        var link_url =$('#bjqdbb_cslj').val('');
        var intro = $('#bjqdbb_jj').val('');
        var state = $('#bjqdbb_qdbm').val(1);
        
        
        
        var url="/Support/add_list" ;
        $('.sp_name').html('新增渠道');
        
        //$('#bjqdxx_qdm').val(info.name);
        // $('#bjqdxx_cjsj').val(info.introduction);
        // $('#bjqdxx_qdbm').val(info.state);  
        $('.bb').show(); 
        $('.dialog_bg').show(); 
        $('#bjqdbb_btn').on('click', function () {
            
        var name =$('#bjqdbb_qdm').val();
        var type =$('#bjqdbb_lx').val();
        var version =$('#bjqdbb_bbh').val();
        var channel_url =$('#bjqdbb_qdwz').val();
        var link_url =$('#bjqdbb_cslj').val();
        var intro = $('#bjqdbb_jj').val();
        var state = $('#bjqdbb_qdbm').val();
        if(($('#bjqdbb_qdm').val())==0){
                alert('名称不能为空');
                return false; 
            }else if($('#bjqdbb_bbh').val()==0){
                alert('版本号不能为空');
                return false;
            }
        $.ajax({
            type: 'post',
            data: {
                name:name,
                type:type,
                version:version,
                channel_url:channel_url,
                link_url:link_url,
                intro:intro,
                state:state

            },
            url: url,
            dataType: 'text',
            success: function (response) {
                alert(response);
                /*if(GetQueryString("id")!=='1'){
                    alert(GetQueryString("id")!=='1');
                    window.location.href="/YMN.php/Admin/Index/index.php#tabs2";
                    
                }else{*/
                    //location.reload();
                    //window.location.href="/YMN.php/Admin/Index/index.php#tabs2";
                    location.reload(true);
                /*}*/
            }
        });
    });
    });
//发布渠道列表
 $('.fbqdlb').on('click', function () {
        var fb = $(this).data('fbqdlb');
        var id = fb.id;
        var name = fb.name;
        
        if(fb.type==1){
            var type = "安卓";
        }else{
            var type = "IOS";
        }
        var version = fb.version;
        var channel_url = fb.channel_url;
        var link_url = fb.link_url;
        var intro = fb.intro;
        
        if(fb.state==1){
            var state = "未开放下载";
        }else{
            var state = "开放下载";
        }
        var url="/Support/release_list" ;
        
       
        if(confirm(name+"为"+type+"渠道,当前状态为'"+state+"'是否发布？")){
            $.ajax({
                type: 'post',
                data: {
                    id:id,
                    name:name,
                    type:type,
                    version:version,
                    channel_url:channel_url,
                    link_url:link_url,
                    intro:intro,
                    state:state

                },
                url: url,
                dataType: 'text',
                success: function (data) {
                    alert(data);
                    //window.location.href="index#tabs2";
                    location.reload(true);
                }
            });
        }else{
            return false;
        }
            
        
        
        
    
    });
$(function(){
       //上传图片
       $("#btnUpload").click(function() {
               alert(ajaxFileUpload());
       });
    });
    function ajaxFileUpload() {
        // 开始上传文件时显示一个图片
        // $("#wait_loading").ajaxStart(function() {
        //     $(this).show();
        // // 文件上传完成将图片隐藏起来
        // }).ajaxComplete(function() {
        //     $(this).hide();
        // });
        var elementIds=["flag"]; //flag为id、name属性名
        $.ajaxFileUpload({
            url: '/YMN.php/Admin/Index/edit_version', 
            type: 'post',
            secureuri: false, //一般设置为false
            fileElementId: 'file', // 上传文件的id、name属性名
            dataType: 'text', //返回值类型，一般设置为json、application/json
            elementIds: elementIds, //传递参数到服务器
            success: function(data, status){  
                alert(data);
            },
            error: function(data, status, e){ 
                alert(e);
            }
        });
        //return false;
    }
    //提交反馈
$('.tjfk').on('click', function () {
        var tjfk = $(this).data('tjfk');
        var url="/Test/response" ;
        $('.sp_name').html('提交反馈-'+tjfk.name+tjfk.version_name);
        var state =$('#mbcs_jg').val('3');
        var intro = $('#mbcs_sm').val('');
        $('.cc').show(); 
        $('.dialog_bg').show(); 
        

        $('#tjfk_btn').on('click', function () {

        
       
        var state =$('#mbcs_jg').val();
        var intro = $('#mbcs_sm').val();

        $.ajax({
            type: 'post',
            data: {
                id:tjfk.id,
               
                intro:intro,
                state:state

            },
            url: url,
            dataType: 'text',
            success: function (response) {
                alert(response);
                //window.location.href="/YMN.php/Admin/Index/index.php#tabs3";
                location.reload(true);
            }
        });
    });
    });
//搜索
/*$('.mbcs_ss').on('click',function(){
    var hid_id = $('#hid_id').val();
    var content = $('#content').val();
    var url="/YMN.php/Admin/Index/index" ;
    $.ajax({
            type: 'post',
            data: {
                hid_id:hid_id,
                content:content
            },
            url: url,
            dataType: 'text',
            success: function (response) {
                alert(response);
                window.location.href="/YMN.php/Admin/Index/index.html/#tabs3";
            }
        });
});*/
//产品列表删除
$('.cplb').on('click',function(){
    var id = $(this).data('cplb');
    var url="/App/delete_cp" ;
    $.ajax({
            type: 'post',
            data: {
                id:id
            },
            url: url,
            dataType: 'text',
            success: function (response) {
                alert(response);
                //window.location.href="/YMN.php/Admin/Index/index.php#tabs5";
                location.reload(true);
            }
        });
});
//账号编辑
$('.xzzh_bj').on('click', function () {

        var info = $(this).data('qxgl');
        $('#xzzh_yhm').val(info.username);
        $('#xzzh_zh').val(info.account);
        //$('#xzzh_mm').val(info.password);
        $('#xzzh_qx').val(info.power);
        $('#xzzh_zt').val(info.status);
        $('.sp_name').html('编辑账号');
        var url="/User/edit_user" ;
        $('.dd').show(); 
        $('.dialog_bg').show(); 
        var id = info.id;
        

        $('#xzzh_btn').on('click', function () {
            var username = $('#xzzh_yhm').val();
            var account = $('#xzzh_zh').val();
            var password = $('#xzzh_mm').val();
            var power = $('#xzzh_qx').val();
            var state = $('#xzzh_zt').val();
            if(username==0){
                alert('名称不能为空');
                return false; 
            }else if(account==0){
                alert('账号不能为空');
                return false;
            }
            $.ajax({
                type: 'post',
                data: {
                    id:id,                                 
                    username:username,
                    account:account,
                    password:password,
                    power:power,
                    state:state

                },
                url: url,
                dataType: 'text',
                success: function (response) {
                    alert(response);
                    //window.location.href="/YMN.php/Admin/Index/index.php#tabs4";
                    location.reload(true);
                }
            });
        });
    
    });
//新增账号
$('.xzzh_xz').on('click', function () {

        
        $('#xzzh_yhm').val('');
        $('#xzzh_zh').val('');
        $('#xzzh_mm').val('');
        $('#xzzh_qx').val('0');
        $('#xzzh_zt').val('1');
        $('.sp_name').html('新增账号');
        var url="/User/edit_user" ;
        $('.dd').show(); 
        $('.dialog_bg').show(); 
        
        

        $('#xzzh_btn').on('click', function () {
            var username = $('#xzzh_yhm').val();
            var account = $('#xzzh_zh').val();
            var password = $('#xzzh_mm').val();
            var power = $('#xzzh_qx').val();
            var state = $('#xzzh_zt').val();
            if(username==0){
                alert('名称不能为空');
                return false; 
            }else if(account==0){
                alert('账号不能为空');
                return false;
            }else if(password==0){
                alert('密码不能为空');
                return false;
            }
            $.ajax({
                type: 'post',
                data: {                                
                    username:username,
                    account:account,
                    password:password,
                    power:power,
                    state:state

                },
                url: url,
                dataType: 'text',
                success: function (response) {
                    alert(response);
                    window.location.href="index?id=3";
                    location.reload(true);
                }
            });
        });
    
    });
$('.return').on('click',function(){
    var tabid=GetQueryString("id");
    
    var tab=window.location.hash;
    if(tab){
        switch (tab)
        {
        case "#tabs2":
          window.location.href="index#tabs2";
          break;
        case "#tabs3":
          window.location.href="index#tabs3";
          break;
        case "#tabs4":
          window.location.href="index#tabs4";;
          break;
        case "#tabs5":
          window.location.href="index#tabs5";
          break;
        
        }
    }else{
        switch (tabid)
        {
        case "1":
          window.location.href="index?id=1";
          break;
        case "2":
          window.location.href="index?id=2";
          break;
        case "3":
          window.location.href="index?id=3";
          break;
        case "4":
          window.location.href="index?id=4";
          break;
        
        }
    }
});
$('#check_btn').on('click',function(){
        $('#error_name').html("<span></span>");
        $('#error_psd').html("<span></span>");
        var username = $('.username').val();

        var password = $('.password').val();
        var url      = '/Public/checklogin';

        //alert(username);
        if(!username){
            $('#error_name').html("<span style='color:red'>账号不为空</span>");
            return false;
        }else if(!password){
            $('#error_psd').html("<span style='color:red'>密码不为空</span>");
            return false;
        }
        $.ajax({
            type:'post',
            data:{
                username:username,
                password:password,

            },
            url:url,
            dataType: 'text',
            success: function (response) {
                    if(response==1){
                        window.location.href="/Test/index?id=2";
                        return false;
                    }else if(response){
                        alert(response);
                        return false;
                    }else{
                        window.location.href="/Index/index?id=0";
                    }
                    
                    
                    
                }
        });
});
/*function sAlert(str){
var msgw,msgh,bordercolor;
msgw=400;//提示窗口的宽度
msgh=100;//提示窗口的高度
titleheight=25 //提示窗口标题高度
bordercolor="#c51100";//提示窗口的边框颜色
titlecolor="#c51100";//提示窗口的标题颜色

var sWidth,sHeight;
sWidth=screen.width;
sHeight=screen.height;

var bgObj=document.createElement("div");
bgObj.setAttribute('id','bgDiv');
bgObj.style.position="absolute";
bgObj.style.top="0";
bgObj.style.background="#cccccc";
bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
bgObj.style.opacity="0.6";
bgObj.style.left="0";
bgObj.style.width=sWidth + "px";
bgObj.style.height=sHeight + "px";
bgObj.style.zIndex = "10000";
document.body.appendChild(bgObj);

var msgObj=document.createElement("div")
msgObj.setAttribute("id","msgDiv");
msgObj.setAttribute("align","center");
msgObj.style.background="white";
msgObj.style.border="1px solid " + bordercolor;
msgObj.style.position = "absolute";
msgObj.style.left = "50%";
msgObj.style.top = "50%";
msgObj.style.font="12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
msgObj.style.marginLeft = "-225px" ;
msgObj.style.marginTop = -75+document.documentElement.scrollTop+"px";
msgObj.style.width = msgw + "px";
msgObj.style.height =msgh + "px";
msgObj.style.textAlign = "center";
msgObj.style.lineHeight ="25px";
msgObj.style.zIndex = "10001";

   var title=document.createElement("h4");
   title.setAttribute("id","msgTitle");
   title.setAttribute("align","right");
   title.style.margin="0";
   title.style.padding="3px";
   title.style.background=bordercolor;
   title.style.filter="progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
   title.style.opacity="0.75";
   title.style.border="1px solid " + bordercolor;
   title.style.height="18px";
   title.style.font="12px Verdana, Geneva, Arial, Helvetica, sans-serif";
   title.style.color="white";
   title.style.cursor="pointer";
   title.innerHTML="关闭";
   title.onclick=function(){
document.body.removeChild(bgObj);
document.getElementById("msgDiv").removeChild(title);
document.body.removeChild(msgObj);
}
   document.body.appendChild(msgObj);
   document.getElementById("msgDiv").appendChild(title);
   var txt=document.createElement("p");
   txt.style.margin="1em 0"
   txt.setAttribute("id","msgTxt");
   txt.innerHTML=str;
   document.getElementById("msgDiv").appendChild(txt);
}
*/
function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}